import type { Extension, HTMLContent, JSONContent } from "@tiptap/core";
import type { CSSProperties, StyleValue } from "vue";

/**
 * 选择器选项接口
 */
export interface SelectOption {
  label: string;
  value: any;
  disabled?: boolean;
}

/**
 * 按钮组件类型
 */
export type ButtonType = "button" | "select" | "popover" | "dropdown";

/**
 * 按钮尺寸类型（图标大小，支持数字如 20，或字符串如 "20px"、"20rem" 等）
 */
export type ButtonSize = number | string;

/**
 * 按钮变体类型
 */
export type ButtonVariant = "default" | "primary" | "secondary";

/**
 * Element Plus 按钮类型
 */
export type ElementButtonType =
  | "primary"
  | "success"
  | "warning"
  | "danger"
  | "info"
  | "text"
  | "";

/**
 * Popover 位置类型
 */
export type PopoverPlacement =
  | "top"
  | "top-start"
  | "top-end"
  | "bottom"
  | "bottom-start"
  | "bottom-end"
  | "left"
  | "left-start"
  | "left-end"
  | "right"
  | "right-start"
  | "right-end";

/**
 * Popover 触发方式类型
 */
export type PopoverTrigger = "click" | "focus" | "hover" | "contextmenu";

/**
 * 工具栏按钮组件 Props 接口
 */
export interface ToolbarButtonProps {
  /** 组件类型 */
  type?: ButtonType;
  /** 图标类名 */
  icon?: string;
  /** 按钮提示文字 */
  title?: string;
  /** 是否隐藏文本 */
  hideText?: boolean;
  /** 快捷键 */
  shortcut?: string;
  /** 自定义提示内容（如果提供，将优先使用此值） */
  tooltip?: string;
  /** 是否是大图标 */
  largeIcon?: boolean;
  /** 是否禁用 */
  disabled?: boolean;
  /** 是否激活状态 */
  active?: boolean;
  /** 按钮变体样式 */
  variant?: ButtonVariant;

  // Element Plus Button 相关属性
  /** Element Plus 按钮类型 */
  elementType?: ElementButtonType;
  /** 是否为朴素按钮 */
  plain?: boolean;
  /** 是否为圆角按钮 */
  round?: boolean;
  /** 是否为圆形按钮 */
  circle?: boolean;

  // Select 相关属性
  /** Select 的值 */
  modelValue?: any;
  /** 占位符 */
  placeholder?: string;
  /** 选项列表 */
  options?: SelectOption[];
  /** 是否可清空 */
  clearable?: boolean;
  /** 是否可搜索 */
  filterable?: boolean;
  /** 是否多选 */
  multiple?: boolean;
  /** Select 宽度 */
  selectWidth?: string;

  // Popover 相关属性
  /** Popover 位置 */
  popoverPlacement?: PopoverPlacement;
  /** Popover 宽度 */
  popoverWidth?: string | number;
  /** Popover 触发方式 */
  popoverTrigger?: PopoverTrigger;
  /** Popover 自定义类名 */
  popperClass?: string;
  popperStyle?: StyleValue;

  // Dropdown 类型相关属性
  /** 下拉箭头图标 */
  dropdownIcon?: string;
  /** 主按钮点击事件 */
  onMainClick?: () => void;
  /** 下拉按钮点击事件 */
  onDropdownClick?: () => void;
}

export type LocaleLabel = string | { en_US: string; zh_CN: string };
export type SupportedLocale = "en-US" | "zh-CN";

export interface PageSize {
  label: LocaleLabel;
  width: number;
  height: number;
  default?: boolean;
}

export interface Font {
  label: LocaleLabel;
  value: string | null;
}

export interface FontSize {
  label: LocaleLabel;
  value: string;
}

export interface LineHeight {
  label: LocaleLabel;
  value: number;
  default?: boolean;
}

export interface Heading {
  label: LocaleLabel;
  value: string;
}

export interface LinkType {
  label: LocaleLabel;
  value: string;
  icon?: string;
}

export interface ListStyle {
  value: string;
  label: string;
  preview: Array<{
    marker: string;
    content: string;
  }>;
}

export interface GraphicSymbol {
  label: LocaleLabel;
  items: string;
}

export interface Emoji {
  label: LocaleLabel;
  items: string;
}

export type ToolbarMenu =
  | "base"
  | "insert"
  | "table"
  | "tools"
  | "page"
  | "export"
  | "advanced"
  | "custom";

export interface ToolbarOptions {
  mode?: "ribbon" | "simple";
}

export type LayoutOption = "web" | "page";

export interface MarginOption {
  left: number;
  right: number;
  top: number;
  bottom: number;
}

export interface WatermarkOption {
  type?: string;
  alpha?: number;
  fontColor?: string;
  fontSize?: number;
  fontFamily?: string;
  fontWeight?: string;
  text?: string;
}

export interface PageOption {
  layout: LayoutOption;
  layouts: LayoutOption[];
  defaultMargin?: MarginOption;
  defaultOrientation?: string;
  defaultBackground?: string;
  showBreakMarks?: boolean;
  showBookmark?: boolean;
  watermark?: WatermarkOption;
  size?: {
    width: number;
    height: number;
    label?: LocaleLabel;
  };
  margin?: MarginOption;
  orientation?: string;
  background?: string;
  header?: boolean;
  footer?: boolean;
  showLineNumber?: boolean;
  showToc?: boolean;
  zoomLevel?: number;
  bodyHeight?: number;
  autoWidth?: boolean;
  preview?: {
    enabled?: boolean;
    laserPointer?: boolean;
  };
}

export interface AutoSaveOptions {
  enabled?: boolean;
  interval?: number;
}

export interface DocumentOptions {
  id?: string;
  title?: string;
  content?: string;
  placeholder?: Record<string, string>;
  enableSpellcheck?: boolean;
  enableMarkdown?: boolean;
  enableBubbleMenu?: boolean;
  enableBlockMenu?: boolean;
  readOnly?: boolean;
  autofocus?: "start" | "end" | "all" | number | boolean | null;
  characterLimit?: number;
  typographyRules?: Record<string, unknown>;
  editorProps?: Record<string, unknown>;
  parseOptions?: Record<string, unknown>;
  autoSave?: AutoSaveOptions;
}

export interface WebPageItem {
  label?: LocaleLabel;
  icon?: string;
  validate?(url: string): boolean;
  transformURL?(url: string): string;
}

export interface Template {
  title: string;
  content: string;
  description?: string;
  value?: string;
  divider?: boolean;
}

export interface ImportWordOptions {
  enabled?: boolean;
  maxSize?: number;
  options?: unknown;
  useCustomMethod?: boolean;
  onCustomImportMethod?: (file: File) => Promise<{
    id: string;
    url: string;
    value: string;
    messages: { type: string; message: string };
  }>;
}

type OnSaveFunction = (
  content: {
    html: HTMLContent;
    json: JSONContent;
    text: string;
  },
  page: PageOption,
  document: DocumentOptions
) => Promise<unknown>;

export interface EditorProps {
  editorKey?: string;
  locale?: SupportedLocale;
  theme?: "light" | "dark";
  height?: string;
  fullscreenZIndex?: number;
  dicts?: {
    pageSizes?: PageSize[];
    fonts?: Font[];
    fontSizes?: FontSize[];
    colors?: string[];
    standardColors?: string[];
    lineHeights?: LineHeight[];
    headings?: Heading[];
    linkTypes?: LinkType[];
    symbols?: GraphicSymbol[];
    emojis?: Emoji[];
    bulletListStyles?: ListStyle[];
    orderedListStyles?: ListStyle[];
  };
  toolbar?: ToolbarOptions;
  page?: PageOption;
  document?: DocumentOptions;
  webPages?: WebPageItem[];
  templates?: Template[];
  cdnUrl?: string;
  shareUrl?: string;
  diagrams?: Record<string, unknown>;
  importWord?: ImportWordOptions;
  user?: Record<string, unknown>;
  extensions?: Extension[];
  disableExtensions?: string[];
  translations?: Record<string, unknown>;
  onSave?: OnSaveFunction;
  onFileUpload?: (file: File) => Promise<{ id: string; url: string }>;
  onFileDelete?: CallableFunction;
  /** 全局默认行高 */
  defaultLineHeight?: number | string;
}
